
import sun.applet.AppletAudioClip;

import javax.swing.*;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @classname 角色类
 * @Author 姬如千泷
 * @Date 2021/4/29  14:25
 * @Version 1.0
 */
class MyCharacter extends JLabel {
    MyCharacter ct_label = this;
    MyTown bg_panel;//背景容器——城镇
    JLabel bg_label;//背景标签——地下城
    MyFrame myFrame;//主屏幕——城镇
    MyMap myMap;//主屏幕——地下城
    MyState myState;//状态栏
    public int pressNum = 0;//按压计数器
    boolean juage_lr;//判断当前向左还是向右,左true
    boolean juage_where=false;//判断当前在城镇还是地下城,false为在地下城
    boolean juage_jump = false;//判断当前是否属于跳跃状态
    boolean juage_attack = false;//判断当前是否属于攻击状态
    boolean juage_run = false;//判断当前是否属于跑步状态
    boolean juage_jumpx = false;//判断当前是否需要跳跃斩击
    boolean juage_stright = false;//判断当前是否属于上下走状态
    boolean juage_skill = false;//判断当前是否正在释放技能
    public int jump_y;//起跳的y坐标
    public volatile int ct_x=100;//角色的x坐标
    public volatile int ct_y=500;//角色的x坐标
    public MyCharacter(MyFrame e,MyTown f) {
        myFrame = e;
        this.setIcon(DateCenter.rstand);
        this.setBounds(ct_x, ct_y, 150, 200);
        this.bg_panel = f;
        bg_panel.add(this,0);
        myState = new MyState(bg_panel);
    }
    public MyCharacter(MyMap e,JLabel f) {
        juage_where=true;
        myMap = e;
        this.setIcon(DateCenter.rstand);
        this.setBounds(ct_x, ct_y, 150, 200);
        this.bg_label =f;
        myMap.add(this,0);
        myState = new MyState(myMap);
    }
    public MyCharacter(int x,int y,int z,MyFrame e,MyTown f) {
        ct_x=x;ct_y=y;
        myFrame = e;
        juage_lr=true;
        this.setIcon(DateCenter.lstand);
        this.setBounds(ct_x, ct_y, 150, 200);
        this.bg_panel = f;
        bg_panel.add(this,0);
        myState = new MyState(z,bg_panel);
    }

    public void ljump() {//向左跳跃
        if(!juage_jump&&!juage_attack&&!juage_skill) {
            jump_y=ct_y+100;
            juage_jump = true;
            new Myjump(2).start();
        }
    }
    public void rjump() {//向右跳跃
        if(!juage_jump&&!juage_attack&&!juage_skill) {
            jump_y=ct_y+100;
            juage_jump = true;
            new Myjump(3).start();
        }
    }
    public void rattack() {//向左攻击
        if(!juage_attack&&!juage_jump&&!juage_skill) {
            juage_attack = true;
            new Myattack(5).start();
        }
    }
    public void lattack() {//向右攻击
        if(!juage_attack&&!juage_jump&&!juage_skill) {
            juage_attack = true;
            new Myattack(4).start();
        }
    }
    public void lmove(int y_move) {//向左移动
        pressNum++;//计算器加一
        if(juage_attack||juage_skill){}
        else if(juage_stright&&!juage_jump){//上下直走状态
            juage_lr = true;//方向设置为左
            ct_y = y_move;
            if(juage_run) {
                this.setIcon(juage(pressNum % 2, 7));
                this.setBounds(ct_x,y_move, 150, 200);
            }
            else {
                this.setIcon(juage(pressNum % 11, 1));
                this.setBounds(ct_x,y_move, 150, 200);
            }
        }
        else if(juage_jump) ct_x -= 60;//跳跃状态下，只移动x坐标
        else if(!juage_where){//如果当前在城镇中
            if (bg_panel.bg_x < 0 && ct_label.ct_x+bg_panel.bg_x < 400) {//判断是否到界面边界
                bg_panel.bg_x += 25;
                bg_panel.move();//移动背景
                myState.move(-25);//移动状态栏
                if(juage_run) this.setIcon(juage(pressNum % 2, 7));
                else this.setIcon(juage(pressNum % 11, 1));
                this.setLocation(ct_x,ct_y);
            }
        }
        if(juage_attack||juage_skill||juage_stright){}
        else if(juage_run) {//跑步状态下
            juage_lr = true;//方向设置为左
            if(!(ct_x<20))ct_x-=25;
            this.setIcon(juage(pressNum % 2, 7));
            this.setBounds(ct_x,ct_y, 150, 200);
        } else{//走路状态下
            juage_lr = true;//方向设置为左
            if(!(ct_x<20))ct_x-=5;
            this.setIcon(juage(pressNum % 11, 1));
            this.setBounds(ct_x,ct_y, 150, 200);
        }
        juage_stright=false;
        if(juage_where) myMap.repaint();
            else myFrame.repaint();//刷新窗体
    }
    public void rmove(int y_move) {//向右移动
        pressNum++;
        if(juage_attack||juage_skill){}
        else if(juage_stright&&!juage_jump){//上下直走状态
            juage_lr = false;
            ct_y = y_move;
            if(juage_run) {
                this.setIcon(juage(pressNum % 2, 8));
                this.setBounds(ct_x,y_move, 150, 200);
            }
            else {
                this.setIcon(juage(pressNum % 11, 0));
                this.setBounds(ct_x,y_move, 150, 200);
            }
        }else if(juage_jump)  ct_x += 60;//跳跃状态下，只移动x坐标
         else if(!juage_where){//如果当前在城镇中
            if (bg_panel.bg_x > -1010 && ct_x > 800) {//判断是否到界面边界
                bg_panel.bg_x -= 25;
                this.setLocation(ct_x, ct_y);
                bg_panel.move();//移动背景
                myState.move(25);
                if (juage_run) this.setIcon(juage(pressNum % 2, 8));
                else this.setIcon(juage(pressNum % 11, 0));
            }
        }
        if(juage_attack||juage_skill||juage_stright){}
        else if(juage_run) {//跑步状态下
            juage_lr = false;
            ct_x+=25;
            this.setIcon(juage(pressNum % 2, 8));
            this.setBounds(ct_x,y_move, 150, 200);
        }else {//走路状态下
            juage_lr = false;
            ct_x+=5;
            this.setIcon(juage(pressNum % 11, 0));
            this.setBounds(ct_x,y_move, 150, 200);
        }
        juage_stright=false;
        if(juage_where) myMap.repaint();
        else myFrame.repaint();//刷新窗体
    }
    public ImageIcon juage(int i, int t) {
        if (t == 1) {//绘制向左走路
            switch (i) {
                case 0:
                    return DateCenter.lmove0;
                case 1:
                    return DateCenter.lmove1;
                case 2:
                    return DateCenter.lmove2;
                case 3:
                    return DateCenter.lmove3;
                case 4:
                    return DateCenter.lmove4;
                case 5:
                    return DateCenter.lmove5;
                case 6:
                    return DateCenter.lmove6;
                case 7:
                    return DateCenter.lmove7;
                case 8:
                    return DateCenter.lmove8;
                case 9:
                    return DateCenter.lmove9;
                case 10:
                    return DateCenter.lmove10;
                default:
                    return null;
            }
        } else if (t == 0) {//绘制向右走路
            switch (i) {
                case 0:
                    return DateCenter.rmove0;
                case 1:
                    return DateCenter.rmove1;
                case 2:
                    return DateCenter.rmove2;
                case 3:
                    return DateCenter.rmove3;
                case 4:
                    return DateCenter.rmove4;
                case 5:
                    return DateCenter.rmove5;
                case 6:
                    return DateCenter.rmove6;
                case 7:
                    return DateCenter.rmove7;
                case 8:
                    return DateCenter.rmove8;
                case 9:
                    return DateCenter.rmove9;
                case 10:
                    return DateCenter.rmove10;
                default:
                    return null;
            }
        } else if (t == 2) {//绘制向左跳跃
            switch (i) {
                case 0:
                    return DateCenter.ljump1;
                case 1:
                    return DateCenter.ljump2;
                case 2:
                    return DateCenter.ljump3;
                case 3:
                    return DateCenter.ljump4;
                case 4:
                    return DateCenter.ljump5;
                case 5:
                    return DateCenter.ljump6;
                case 6:
                    return DateCenter.ljump7;
                default:
                    return null;
            }
        } else if (t == 3) {//绘制向右跳跃
            switch (i) {
                case 0:
                    return DateCenter.rjump1;
                case 1:
                    return DateCenter.rjump2;
                case 2:
                    return DateCenter.rjump3;
                case 3:
                    return DateCenter.rjump4;
                case 4:
                    return DateCenter.rjump5;
                case 5:
                    return DateCenter.rjump6;
                case 6:
                    return DateCenter.rjump7;
                default:
                    return null;
            }
        } else if (t == 4) {//绘制向左攻击
            switch (i) {
                case 0:
                    return DateCenter.lattack1;
                case 1:
                    return DateCenter.lattack2;
                case 2:
                    return DateCenter.lattack3;
                case 3:
                    return DateCenter.lattack4;
                case 4:
                    return DateCenter.lattack5;
                case 5:
                    return DateCenter.lattack6;
                default:
                    return null;
            }
        } else if (t == 5) {//绘制向右攻击
            switch (i) {
                case 0:
                    return DateCenter.rattack1;
                case 1:
                    return DateCenter.rattack2;
                case 2:
                    return DateCenter.rattack3;
                case 3:
                    return DateCenter.rattack4;
                case 4:
                    return DateCenter.rattack5;
                case 5:
                    return DateCenter.rattack6;
                default:
                    return null;
            }
        } else if(t==7){
            switch (i) {//向左跑步
                case 0:
                    return DateCenter.lrun1;
                case 1:
                    return DateCenter.lrun2;
                default:
                    return null;
            }
        }else if(t==8){
            switch (i) {//向右跑步
                case 0:
                    return DateCenter.rrun1;
                case 1:
                    return DateCenter.rrun2;
                default:
                    return null;
            }
        }else if(t==9){//向左跳跃斩击
            switch (i) {
                case 0:
                    return DateCenter.ljumpx1;
                case 1:
                    return DateCenter.ljumpx2;
                case 2:
                    return DateCenter.ljumpx3;
                case 3:
                    return DateCenter.ljumpx4;
                case 4:
                    return DateCenter.ljumpx5;
                default:
                    return null;
            }
        }else {
            switch (i) {//向右跳跃斩击
                case 0:
                    return DateCenter.rjumpx1;
                case 1:
                    return DateCenter.rjumpx2;
                case 2:
                    return DateCenter.rjumpx3;
                case 3:
                    return DateCenter.rjumpx4;
                case 4:
                    return DateCenter.rjumpx5;
                default:
                    return null;
            }
        }
    }
    class Myjump extends Thread{//跳跃线程
        int i;
        public Myjump(int i) {
            this.i = i;
        }
        @Override
        public void run() {
            try {
                for (int i = 1; i < 8; i++) {
                    if(ct_label.juage_jumpx){
                        if(ct_label.juage_lr)
                            new Myjumpx(9).start();
                        else new Myjumpx(10).start();
                        Thread.sleep(500);
                    }
                    if(i==1){ct_y-=50;ct_label.setBounds(ct_x,ct_y,100,250);}
                    else if(i==2){ct_y-=50;ct_label.setBounds(ct_x,ct_y,100,200);}
                    else if(i==3){ct_y-=50;ct_label.setBounds(ct_x,ct_y,100,200);}
                    else if(i==4){ct_y-=50;ct_label.setBounds(ct_x,ct_y,150,200);}
                    else if(i==5){ct_y+=50;ct_label.setBounds(ct_x,ct_y,150,200);}
                    else if(i==6){ct_y+=50;ct_label.setBounds(ct_x,ct_y,150,250);}
                    else {ct_y+=100;ct_label.setBounds(ct_x,ct_y,150,200);}
                    ct_label.setIcon(juage(i-1,this.i));
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(ct_label.juage_lr)
                ct_label.setIcon(DateCenter.lstand);
            else ct_label.setIcon(DateCenter.rstand);
            juage_jump=false;
        }
    }
    class Myattack extends Thread{//跳跃线程
        int i;
        public Myattack(int i) {
            this.i = i;
        }
        @Override
        public void run() {
            try {
                for (int i = 1; i < 7; i++) {
                    ct_label.setIcon(juage(i-1,this.i));
                    if(i==1) {
                        new MySpecial(ct_label).poised();
                        Thread.sleep(500);
                    }
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(ct_label.juage_lr)
                ct_label.setIcon(DateCenter.lstand);
            else ct_label.setIcon(DateCenter.rstand);
            juage_attack=false;
        }
    }
    class Myjumpx extends Thread{//跳跃斩击线程
        int i;
        public Myjumpx(int i) {
            this.i = i;
        }
        @Override
        public void run() {
            try {
                for (int i = 1; i < 6; i++) {
                    ct_label.setSize(300,300);
                    ct_label.setIcon(juage(i-1,this.i));
                    if(i==1) {
                        Thread.sleep(200);
                        if(ct_label.juage_lr)
                            new Mybullet(ct_x-45,ct_y+55,ct_label,0).start();
                        else new Mybullet(ct_x+90,ct_y+55,ct_label,0).start();
                    }
                    Thread.sleep(50);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            juage_jumpx=false;
        }
    }
}
